package com.kuyou.ddhm_tv.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.clj.fastble.data.BleDevice;

import com.kuyou.ddhm_tv.R;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

public class BleDeviceAdapter extends RecyclerView.Adapter<BleDeviceAdapter.ViewHolder> implements View.OnClickListener {
    protected List<BleDevice> list =null;


    public BleDeviceAdapter(Context context) {

    }

    @NonNull
    @NotNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull @NotNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.controller_item, parent, false);

        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull @NotNull ViewHolder holder, int position) {

        holder.name.setText(list.get(position).getName());
        holder.delete.setTag(position);
    }


    public void setList(List<BleDevice> list) {
        this.list = list;
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    @Override
    public void onClick(View view) {


        int position = -1;
        if (view.getTag() != null) {
            position = (int) view.getTag();
        }
        if (this.onItemClick != null) {
            if (position != -1) {
                onItemClick.OnClik(list.get(position));

            }
        }
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        public TextView address,name;
        public ImageView delete;

        public ViewHolder(@NonNull @NotNull View itemView) {
            super(itemView);
            name = itemView.findViewById(R.id.name);
            delete =itemView.findViewById(R.id.icon);
            delete.setImageResource(R.mipmap.equipm);
            delete.setOnClickListener(BleDeviceAdapter.this);
        }
    }

    public OnItemClick onItemClick;

    public void setOnItemClick(OnItemClick onItemClick) {
        this.onItemClick = onItemClick;
    }

    public interface OnItemClick {
        void OnClik(BleDevice bleDevice);
    }

    public void addItem(BleDevice bleDevice){
        if(list== null){
            list = new ArrayList<>();
        }

        if(list.contains(bleDevice)){
            return;
        }
        list.add(bleDevice);
    }


    public void removeList(){
        if(list!=null){
            list.clear();
        }

    }
}
